package com.example.myapplication.ContentTest;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ListView;
import androidx.appcompat.widget.SearchView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.example.myapplication.ArrayAdapterInclude.DictionaryArrayListAdapter;
import com.example.myapplication.R;
import java.util.ArrayList;

public class DictionaryInfoActivity extends AppCompatActivity {
  ListView listView;
  SearchView searchView;
  public ArrayList<DictionaryInfo> originalDataList;
  DictionaryArrayListAdapter adapter;


  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dictionary_list_layout);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("生词列表");
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    Init();
  }

  private void Init() {
    listView = findViewById(R.id.listView);
    searchView = findViewById(R.id.search_view);
    searchView.setQueryHint("请输入生词");
    originalDataList = GetVocabulary();
    adapter = new DictionaryArrayListAdapter(this, this, originalDataList);

    listView.setAdapter(adapter);

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
      @Override public boolean onQueryTextSubmit(String query) {
        if (query.isEmpty()) {
          adapter.setListData(originalDataList);

        }
        return true;
      }

      @Override public boolean onQueryTextChange(String newText) {
        if (newText.isEmpty()) {

          adapter.setListData(originalDataList);
        }
        System.out.println("原数据的大小为" + originalDataList.size());
        System.out.println("newText的值为" + newText);
        DataFilter(newText);
        return true;
      }
    });
  }

  private void DataFilter(String string) {

    ArrayList<DictionaryInfo> arrayList;
    arrayList = SelectData(string);

    adapter.setListData(arrayList);
  }

  public ArrayList<DictionaryInfo> GetVocabulary() {
    ArrayList<DictionaryInfo> dictionaryInfoArrayList = new ArrayList<>();
    ContentResolver contentResolver = getContentResolver();
    Uri uri = VocabularyProvider.CONTENT_URI;
    Cursor cursor = contentResolver.query(uri, null, null, null, null);
    if (cursor != null) {
      while (cursor.moveToNext()) {
        @SuppressLint("Range") String word =
            cursor.getString(cursor.getColumnIndex(VocabularyProvider.COLUMN_WORD));
        @SuppressLint("Range") String illustration =
            cursor.getString(cursor.getColumnIndex(VocabularyProvider.COLUMN_ILLUSTRATION));

        DictionaryInfo dictionaryInfo = new DictionaryInfo(word, illustration);
        dictionaryInfoArrayList.add(dictionaryInfo);
      }
    }
    return dictionaryInfoArrayList;
  }

  private ArrayList<DictionaryInfo> SelectData(String query) {

    ArrayList<DictionaryInfo> data = new ArrayList<>();
    for (DictionaryInfo dictionaryInfo : originalDataList) {
      if (dictionaryInfo.getWord().toLowerCase().contains(query.toLowerCase())) {

        data.add(dictionaryInfo);
      }
    }

    return data;

  }


  @Override

  public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        GoBack();
    }
    return true;
  }

  private void GoBack() {
    finish();
  }

  public void UpdateOriginData() {
    //originalDataList.clear();
    originalDataList =GetVocabulary();
  }

  @Override protected void onRestart() {
    super.onRestart();
    UpdateOriginData();
    adapter.setListData(originalDataList);
    System.out.println("已更新数据");
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    UpdateOriginData();
    adapter.setListData(originalDataList);
    System.out.println("已更新数据");
  }
}
